package dao;

import gui.PublicMethod;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import model.CommonList;
import model.Student;
import model.Transition_Student;

public class DaoImp_Quit implements IDao_Quit {
	private CommonList common;
	private DaoMethod method;
	private PublicMethod me;

	public DaoImp_Quit() {
		common = CommonList.getInstance();
		method = new DaoMethod();
		me = new PublicMethod();
	}

	public List<Transition_Student> getStuTransRecord(Student student) {
		String name = student.getName();
		String exe = "select ts.id,ts.date_transition,d.number from transition_student ts,dorm d,student s where ts.student_id="
				+ common.getStudentName().get(name)
				+ " and s.isout=0 and d.id=ts.new_dorm_id and ts.student_id=s.id";
		Vector<Vector<Object>> vectors = method.stQuery(exe);
		List<Transition_Student> tsList = new ArrayList<Transition_Student>();
		for (Vector<Object> vector : vectors) {
			Transition_Student transitionStudent = new Transition_Student();
			transitionStudent.setId(Integer.parseInt(vector.get(0).toString()));
			transitionStudent.setName(name);
			transitionStudent.setDate_transition(Date.valueOf(vector.get(1)
					.toString()));
			transitionStudent.setDormNUmber(vector.get(2).toString());
			tsList.add(transitionStudent);
		}
		return tsList;
	}

	public float getCostPerDay(String dormNumber, Date lastDayOfMonth) {
		String exe = "select average from metercost_view where dormNumber='"
				+ dormNumber + "' and date_type_in='" + lastDayOfMonth + "'";
		try {
			return Float.valueOf(method.stQuery(exe).get(0).get(0).toString());
		} catch (ArrayIndexOutOfBoundsException e) {
			exe = "select average from metercost_view where dormNumber='"
					+ dormNumber + "' and date_type_in='"
					+ me.lastDateOfLastMonth(lastDayOfMonth) + "'";
			try {
				return Float.valueOf(method.stQuery(exe).get(0).get(0)
						.toString());
			} catch (ArrayIndexOutOfBoundsException e1) {
				return -1f;
			}
		}
	}

	public int studentQuit(Student student) {
		String exe = "update student set isout=1,debt=" + student.getDebt()
				+ " where id=" + student.getId();
		return method.stUpdate(exe);
	}

}
